
package com.tengniu.api.thread;

import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;

import java.util.concurrent.*;

public class PPThreadPool extends ThreadPoolExecutor {
    public static final int MAX_CAPACITY = 4096;
    public static final PPThreadPool PUBLIC_POOL8 = new PPThreadPool(8, 16, 60L, "publicPool8");
    public static final PPThreadPool PUBLIC_POOL128 = new PPThreadPool(128, 256, 60L, "publicPoo128");

    public Future<?> submit2(Runnable task) throws PPThreadPool.ThreadPoolFullException {
        try {
            Runnable ttlRunnable = TtlRunnable.get(task);
            return super.submit(ttlRunnable);
        } catch (RejectedExecutionException var3) {
            throw new PPThreadPool.ThreadPoolFullException();
        }
    }

    public <T> Future<T> submit2(Runnable task, T result) throws PPThreadPool.ThreadPoolFullException {
        try {
            Runnable ttlRunnable = TtlRunnable.get(task);
            return super.submit(ttlRunnable, result);
        } catch (RejectedExecutionException var4) {
            throw new PPThreadPool.ThreadPoolFullException();
        }
    }

    public <T> Future<T> submit2(Callable<T> task) throws PPThreadPool.ThreadPoolFullException {
        try {
            Callable ttlCallable = TtlCallable.get(task);
            return super.submit(ttlCallable);
        } catch (RejectedExecutionException var3) {
            throw new PPThreadPool.ThreadPoolFullException();
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    public Future<?> submit(Runnable task) {
        throw new IllegalStateException("请使用submitE(Runnable task)方法！");
    }

    /**
     * @deprecated
     */
    @Deprecated
    public <T> Future<T> submit(Runnable task, T result) {
        throw new IllegalStateException("请使用submit(Runnable task, T result)方法！");
    }

    /**
     * @deprecated
     */
    @Deprecated
    public <T> Future<T> submit(Callable<T> task) {
        throw new IllegalStateException("请使用submit(Callable<T> task)方法！");
    }

    public void execute(Runnable command) {
        Runnable ttlRunnable = TtlRunnable.get(command);
        super.execute(ttlRunnable);
    }

    public PPThreadPool(int corePoolSize, int maximumPoolSize, long aliveSecond, String threadName) {
        super(corePoolSize, maximumPoolSize, aliveSecond, TimeUnit.SECONDS, new LinkedBlockingQueue(4096), new PPThreadFactory(threadName), new AbortPolicy());
    }

    public PPThreadPool(int corePoolSize, int maximumPoolSize, long aliveSecond, String threadName, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, aliveSecond, TimeUnit.SECONDS, workQueue, new PPThreadFactory(threadName), new AbortPolicy());
    }

    public PPThreadPool(int corePoolSize, int maximumPoolSize, long aliveSecond, String threadName, RejectedExecutionHandler reh) {
        super(corePoolSize, maximumPoolSize, aliveSecond, TimeUnit.SECONDS, new LinkedBlockingQueue(4096), new PPThreadFactory(threadName), reh);
    }

    private PPThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    private PPThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    private PPThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public static class NotAllowOperationException extends RuntimeException {
        public NotAllowOperationException() {
        }
    }

    public static class ThreadPoolFullException extends Exception {
        public ThreadPoolFullException() {
        }
    }
}
